﻿package Classes.Player{
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.filters.GlowFilter;
	import flash.media.*;
	
	import Classes.*;
	import Classes.Utils.*;
	import Classes.Elements.*;
	import Classes.Weapons.*;
	import Classes.Weapons.Bullets.*;
	import Classes.Weapons.Rockets.*;
	import Classes.Layers.*;
	import Classes.Interface.*
	import Classes.Sounds.GameSound;
	
	public class Fighter extends Aircraft implements ILaunch{		
		//键盘设置
		private var keysets:KeySets;
		//横向速度
		private var hspeed:Number;
		//纵向速度
		private var vspeed:Number;			
		//火炮数量
		private var _numguns:int;
		//火炮最大数量
		private var maxgunnum:int = 10;
		//一次发射的火箭武器数量
		private var _numrockets:int;
		//火箭武器类型
		private var _rockettype:Object;
		//剩余火箭武器总数
		public var count:int;
		//能量
		private var _energy:Number;
		//最大能量
		private var fullenergy:Number;
		//防护罩
		private var shield:MovieClip;
		//是否正在使用防护罩
		private var isShielding:Boolean;
		//发光滤镜
		private var glow:GlowFilter;
		//构造函数
		public function Fighter(){					
			life = GameSettings.FullLife;
			count = 0;
			hp = life;			
			fullenergy = GameSettings.FullEnergy;
			energy = fullenergy;
			numGuns = 4;
			numRockets = 2;
			execution = 50;
			excursion = 1;
			speed = 10;
			hspeed = speed * .8;
			vspeed = speed * .6;
			fireable = true;
			weaponType = WeaponType.BeelineBullet;
			rocketType = WeaponType.Nothing;
			isShielding = false;
			
			createShield();
			createGlow();
		}
		//创建防护罩
		private function createShield(){
			shield = new Shield();
			shield.master = this;
			shield.y = -5;
			shield.canShield = false;
		}
		//创建发光滤镜
		private function createGlow(){
			glow = new GlowFilter();
			glow.color = 0x66FFFF;
			glow.alpha = 2;
			glow.blurX = 4;
			glow.blurY = 4;
			this.alpha = .8;
			this.filters = [glow];
		}
		//重载onEnterFrame方法
		override protected function onEnterFrame(e:Event):void{
			inc++;
			//当计数器小于100时，飞机处于无敌状态
			if(inc<100){
				this.hurtable = false;
			}
			//回到正常状态
			else{
				this.filters = [];
				this.alpha = 1;
				if(!isShielding)
					this.hurtable = true;
			}
			move();
			fire();
			if(energy < GameSettings.FullEnergy){
				energy += .01;
			}
		}	
		//重载move方法
		override protected function move():void{
			//左、右方向键没有被同时按下
			if(!PopKeys.isDown(keysets.LEFT) && !PopKeys.isDown(keysets.RIGHT)){				
				this.gotoAndStop("normal");	
				hstate=0;
				if(tmpxspeed>0)
					tmpxspeed-=excursion;
				else if(tmpxspeed<0)
					tmpxspeed += excursion;
				else 
					tmpxspeed = 0;
			}
			//上、下方向键没有被同时按下
			if(!PopKeys.isDown(keysets.UP) && !PopKeys.isDown(keysets.DOWN)){				
				vstate=0;
				if(tmpyspeed>0)
					tmpyspeed-=excursion;
				else if(tmpyspeed<0)
					tmpyspeed += excursion;
				else
					tmpyspeed = 0;
			}
			//向左移动
			if(PopKeys.isDown(keysets.LEFT)){
				hstate = -1;
				if(tmpxspeed>0-hspeed)
					tmpxspeed += hstate*excursion;			
				this.gotoAndStop("left");
			}	
			//向右移动
			if(PopKeys.isDown(keysets.RIGHT)){
				hstate = 1;
				if(tmpxspeed<hspeed)
					tmpxspeed += hstate*excursion;				
				this.gotoAndStop("right");				
			}
			//向上移动
			if(PopKeys.isDown(keysets.UP)){
				vstate = -1;
				if(tmpyspeed>0-vspeed)
					tmpyspeed += vstate*excursion;
						
			}
			//向下移动
			if(PopKeys.isDown(keysets.DOWN)){
				vstate = 1;
				if(tmpyspeed<vspeed)
					tmpyspeed += vstate*excursion;			
			}
			//漂移计算
			if(Math.abs(tmpxspeed) <= excursion*.5)
				tmpxspeed = 0;
			if(Math.abs(tmpyspeed) <= excursion*.5)
				tmpyspeed = 0;
			this.x += tmpxspeed;
			this.y += tmpyspeed;	
			//限制移动范围
			/*
			if(this.x < moverange[0] + this.width/2)
				this.x = moverange[0] + this.width/2;
			if(this.x > moverange[1] - this.width/2)
				this.x = moverange[1] - this.width/2;
			if(this.y < moverange[2] + this.height/2)
				this.y = moverange[2] + this.height/2;
			if(this.y > moverange[3] - this.height/2)
				this.y = moverange[3] - this.height / 2;
			*/	
			if(this.x < moverange[0] + this.width/2+10)
				this.x = moverange[0] + this.width/2+10;
			if(this.x > moverange[1] - this.width/2-10)
				this.x = moverange[1] - this.width/2-10;
			if(this.y < moverange[2] + this.height/2+10)
				this.y = moverange[2] + this.height/2+10;
			if(this.y > moverange[3] - this.height/2-10)
				this.y = moverange[3] - this.height /2-10;
				
			//trace(moverange[0],moverange[1],moverange[2],moverange[3]);
		}
		//键盘设置属性
		public function set keySets(value:KeySets):void{
			keysets = value;
			addEventListener(Event.ENTER_FRAME,onEnterFrame,false,0,true);
		}
		//开火
		override public function fire():void{
			if(!this.fireable)
				return;				
			//发射炮弹和火箭武器
			if(PopKeys.isDown(keysets.FIRE)){
				shoot();
				launch();
			}
			//释放防护罩
			if(PopKeys.isDown(keysets.THUNDER)){
				showShield();
			}
		}
		//发射炮弹
		public function shoot():void{
			if(inc%weaponType.interval == 0){
				GameSound.BeelineBulletShoot.play();
				for(var i:int=0;i<numGuns;i++){
					var bb:BeelineBullet = new BeelineBullet();
					bb.aTargets = this.aTargets;
					bb.sender = this;
					if(i < 4){						
						bb.x = this.x -(i- (numGuns>4?4:numGuns)/2 + .5) * 4;
						bb.y = this.y;
					}else{
						var angle:Number = ((i%2)*30 - 15)*(Math.floor(i/2)-1);
						bb.angle = angle;
						bb.execution *= Math.abs(angle)/90;
						bb.alpha = 1 - Math.abs(angle)/90;
						bb.x = this.x;
						bb.y = this.y;
					}					
					bulletLayer.addChild(bb);
				}
			}
		}
		//发射火箭武器
		public function launch():void{
			if(rocketType != WeaponType.Nothing){
				if(inc%rocketType.interval==0){
					createMissile();
					this.count --;
					if(this.count<=0){
						this.rocketType = WeaponType.Nothing;
						this.count = 0;						
					}
				}
			}
		}
		//创建火箭武器
		private function createMissile():void{
			for(var i:int=0;i<numRockets;i++){
				var sw:Sidewinder = new Sidewinder();
				sw.sender = this;
				sw.moverange = moverange;
				sw.aTargets = this.aTargets;
				sw.x = this.x -(i- numRockets/2 + .5) * 8;
				sw.y = this.y;
				bulletLayer.addChild(sw);		
			}
		}
		//显示防护罩
		private function showShield():void{
			if(shield.shieldable){
				isShielding = true;
				shield.canShield = true;
				this.filters = [];
			}
		}
		//重设
		public function reset():void{
			inc=0;
			this.alpha = .8;
			isShielding = false;
			shield.canShield = false;
			hp = life;
			fireable = true;
			hurtable = true;
			energy = fullenergy;
			this.filters = [glow];
		}
		//火箭武器类型属性
		public function set rocketType(value:Object):void{
			_rockettype = value;
			count = value.num;
		}			
		public function get rocketType():Object{
			return _rockettype;
		}		
		//能量属性
		public function set energy(value:Number):void{
			_energy = value>fullenergy?fullenergy:value;
		}
		public function get energy():Number{
			return _energy;
		}
		//火炮数量属性
		public function set numGuns(value:int):void{
			_numguns = value>maxgunnum?maxgunnum:value;
		}
		public function get numGuns():int{
			return _numguns;
		}		
		//火箭武器数量属性
		public function set numRockets(value:int):void{
			_numrockets = value;
		}
		public function get numRockets():int{
			return _numrockets;
		}		
	}
}